<template>
  <el-container class="home-container">
    <!--头部布局-->
    <el-header>
      <div>
        <img src="../assets/logo.jpg" alt class="logo_img"/>
        <span>运动管理平台</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <!--主题部分-->
    <el-container>
      <!--侧边布局-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapase"> ||| </div>
<!--       :collapse-transition 关闭element-ui的动画 -->
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff"
                 unique-opened :collapse="isCollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="activePath">
          <!-- 一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObject[item.id]"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 二级菜单-->
            <el-menu-item :index="it.path" v-for="it in item.subMenuList" :key="it.id"
                          @click="saveNavState(it.path)">
              <template slot="title">
                <i :class="iconsObject[it.id]"></i>
                <span>{{ it.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主体布局-->
      <el-main>
<!--        {{ menuList }}-->
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //菜单列表
      menuList: [],
      //判断伸缩栏
      isCollapse: false,
      //logo图标选择
      iconsObject: {
        '100':'el-icon-user-solid',
        '200':'el-icon-user',
        '101':'el-icon-cloudy',
        '102':'el-icon-star-on',
        '103':'el-icon-baseball',
        '104':'el-icon-s-goods',
        '201':'el-icon-s-promotion',
        '202':'el-icon-s-ticket',
        '203':'el-icon-mobile',
      },
      //默认路径
      activePath: '',
    }
  },
  //创建时候调用
  created() {
    //调用查询menuList方法
    this.getMenuList();
    //取session赋值给默认路径activePath
    this.activePath=window.sessionStorage.getItem("itPath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear(); //清除的session
      this.$router.push("/login"); //回到首页
    },
    //获取导航菜单方法
    async getMenuList() {
      const {data: res} = await this.$http.get("/menus");
      console.log(res);
      if (res.flag != 200) return this.$message.error("获取列表失败！"); //访问失败跳转信息
      this.menuList = res.menus; //访问成功将得到的值传到前端
    },
    //控制伸缩
    toggleCollapase() {
      this.isCollapse = !this.isCollapse;
    },
    //侧边栏选择
    saveNavState(itPath) {
      //存入session中，在页面加载时候赋值给this.activePath
      window.sessionStorage.setItem("itPath",itPath);
      this.activePath=itPath;
    },
  }
}
</script>

<style lang="less" scoped>
//头部样式
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between; // 左右贴边
  padding-left: 0%; // 左边界
  align-items: center; // 水平
  color: #fff;
  font-size: 20px;

  > div { //左侧div加布局
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

//侧边栏样式
.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none; // 对其右边框
  }
}

//主题样式
.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

.logo_img {
  width: 20%;
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; // 显示鼠标指针为：小手
}
</style>